package com.qiandw.enterprise.credit.controller

import com.qiandw.enterprise.credit._
import com.qiandw.enterprise.credit.service.{EasyUISrv, ModuleSrv}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.{PathVariable, RequestMapping, ResponseBody}
import java.util.List
import javax.servlet.http.HttpServletResponse

import com.qiandw.enterprise.credit.common.SysResp
import com.qiandw.enterprise.credit.entity.Module
import com.qiandw.enterprise.credit.model.Tree
import org.springframework.web.servlet.ModelAndView

/**
  * Created by Draven on 2018/7/9.
  * Email:hjx@qiandw.com
  */
@Controller
@RequestMapping(value = Array("/management"))
class ModuleController extends CommonController {

  @Autowired
  var moduleSrv: ModuleSrv = _

  @Autowired
  var easyUISrv: EasyUISrv = _

  @RequestMapping(value = Array("/module"))
  def toModulePage(): ModelAndView = {
    toPage("/module/main")
  }

  @RequestMapping(value = Array("/module/getMenu"))
  @ResponseBody
  def getMenu(response: HttpServletResponse): List[Module] = {
    response.setHeader("Pragma", "No-cache") //设置响应头信息，告诉浏览器不要缓存此内容
    response.setHeader("Cache-Control", "no-cache")
    val modules = moduleSrv.list1LevelModules()
    modules
  }

  @RequestMapping(value = Array("/module/tree"))
  @ResponseBody
  def getMenuTree(): List[Tree] = {
    val list = easyUISrv.getModuleTree
    list
  }

  @RequestMapping(value = Array("/module/treeGrid"))
  @ResponseBody
  def getTreeGrid(): String = {
    val resp = easyUISrv.getModuleTreeGrid
    resp
  }

  @RequestMapping(value = Array("/module/addOrEdit"), produces = Array("application/json;charset=UTF-8"))
  @ResponseBody
  def addModule(method: String, module: Module): SysResp = {
    method match {
      case "add" =>{
        moduleSrv.addModule(module)
    }
      case "edit"=>
        moduleSrv.updateModule(module)
    }
    SysResp(SYS_MSG_SUCCESS)
  }

  @RequestMapping(value = Array("/module/remove/{id}"), produces = Array("application/json;charset=UTF-8"))
  @ResponseBody
  def removeModule(@PathVariable("id") id: Integer): SysResp = {
    moduleSrv.removeModule(id)
    SysResp(SYS_MSG_SUCCESS)
  }
}
